<template>
  <div :class="className" :style="{height:height,width:width}"/>
</template>

<script>
let echarts = require('echarts/lib/echarts')

require("echarts/theme/macarons"); // echarts theme
import resize from "./mixins/resize";
import {debounce} from '@/utils'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "590px",
    },
    autoResize: {
      type: Boolean,
      default: true,
    },
    chartData: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        if (this.chart) this.setOptions(val);
      },
    },
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, "macarons");
      this.setOptions(this.chartData);
    },
    setOptions(chartData) {
      let colorArr = [
        "#2F9323",
        "#D9B63A",
        "#2E2AA4",
        "#9F2E61",
        "#4D670C",
        "#BF675F",
        "#1F814A",
        "#357F88",
        "#673509",
        "#310937",
        "#1B9637",
        "#F7393C",
      ];

      this.chart.setOption({
        xAxis: {
          data: chartData.xData,
          boundaryGap: false,
          axisTick: {
            show: false,
          },
        },
        grid: {
          left: 10,
          right: 10,
          bottom: 20,
          top: 30,
          containLabel: true,
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "cross",
          },
          padding: [5, 10],
        },
        yAxis: {
          axisTick: {
            show: false,
          },
        },
        legend: {
          data: chartData.legendData,
        },
        series: [
          {
            name: "总用户",
            itemStyle: {
              normal: {
                color: "#2F9323",
                lineStyle: {
                  color: "#2F9323",
                  width: 2,
                },
              },
            },
            smooth: true,
            type: "line",
            data: chartData.total,
            animationDuration: 2800,
            animationEasing: "cubicInOut",
          }, {
            name: "新用户",
            itemStyle: {
              normal: {
                color: "#2E2AA4",
                lineStyle: {
                  color: "#2E2AA4",
                  width: 2,
                },
              },
            },
            smooth: true,
            type: "line",
            data: chartData.newer,
            animationDuration: 2800,
            animationEasing: "cubicInOut",
          },
          {
            name: "支付人数",
            itemStyle: {
              normal: {
                color: "#9F2E61",
                lineStyle: {
                  color: "#9F2E61",
                  width: 2,
                },
              },
            },
            smooth: true,
            type: "line",
            data: chartData.pay,
            animationDuration: 2800,
            animationEasing: "cubicInOut",
          },
          {
            name: "活跃人数",
            itemStyle: {
              normal: {
                color: "#D9B63A",
                lineStyle: {
                  color: "#D9B63A",
                  width: 2,
                },
              },
            },
            smooth: true,
            type: "line",
            data: chartData.active,
            animationDuration: 2800,
            animationEasing: "cubicInOut",
          },
          {
            name: "退款人数",
            itemStyle: {
              normal: {
                color: "#4D670C",
                lineStyle: {
                  color: "#4D670C",
                  width: 2,
                },
              },
            },
            smooth: true,
            type: "line",
            data: chartData.refund,
            animationDuration: 2800,
            animationEasing: "cubicInOut",
          },
        ],
      });
    },
  },
};
</script>
